﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Funds
{
    public partial class BankListSelect : BasePage
    {
        private YX_BANK_LISTBLL bll = new YX_BANK_LISTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDate.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");

                txtEnd.Value = DateTime.Now.ToString("yyyy-MM-dd");

                ViewState["SortOrder"] = "n_detail_listid";
                ViewState["OrderDire"] = " desc ";

                //BindData(true);
                Bind();
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow1.Rows.Count; i++)
            {
                if (((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).ToolTip;
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");

                        //string url = "QueueEdit.aspx?ID=" + pid;
                        //Response.Redirect(url);
                        return;
                    }
                }
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void Bind()
        {
            string whereStr = "";
            //录入类型
            if (ddlCheckType.SelectedValue != "-1")
            {
                whereStr += " AND t.N_TYPE=" + ddlCheckType.SelectedValue;
            }
            //公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " and a.company_id=" + FORM_COMPANY_ID.Value;
                /*whereStr += @"  and c.cmy_id in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = "+FORM_COMPANY_ID.Value+@"
connect by prior cp.cmy_id = cp.cmy_parentid) ";*/
            }
            else
            {
                whereStr += @"  and c.cmy_id in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            if (txtDate.Value.Trim() != "" && txtEnd.Value.Trim() != "")
            {
                whereStr += @"   and TRUNC(d.d_trandate) between
       TO_DATE('" + txtDate.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + txtEnd.Value + @"', 'YYYY-MM-DD') ";
            }

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = @"  and c.cmy_id in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + CurrentUserCompanyID + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = bll.GetRecordCount1(Where, lognUser.USER_NAME);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = bll.GetListByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            BindData(true);
        }
    }
}